package com.saodisheng.controller;


import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.saodisheng.interceptor.FrontAuthInterceptor;
import com.saodisheng.model.*;
import com.saodisheng.service.*;
import org.springframework.beans.factory.annotation.Autowired;

@Before({FrontAuthInterceptor.class})
public class HomeController extends BaseController {
	@Autowired
	private NewsFeedService feedSrv;
	@Autowired
	private BlogService blogSrv;
	@Autowired
	private CommentService commentSrv;
	@Autowired
	private HomeService homeSrv;
	@Autowired
	private MessageService msgSrv;
	@Autowired
	private RemindService remindSrv;
	@Autowired
	private ReferMeService referMeService;
	@Autowired
	private RemindService remindService;
	
	public void index() {
		if (getPara(0) == null) {
			setAttr("page", "newfeeds");
		} else {
			setAttr("page", getPara(0));
		}
		String page = getPara(0);
		int cUerId = getLoginAccountId();
		//getPara(0)包括newfeeds,posts,comments,favors,follows,fans,notifies
		//动态
		if ((page == null) || (page.equals("newfeeds"))) {
			//包括自己和自己关注的人的动态
			Page<NewsFeed> newsFeedPage = feedSrv.paginate(cUerId, getParaToInt("p", 1));
			setAttr("newsFeedPage", newsFeedPage);
			//用于标识当前页面为动态页
			setAttr("page", "newfeeds");
		}
		//我的文章
		else if (page.equals("posts")) {
			Page<Posts> postPage = blogSrv.getPosts(getParaToInt("p", 1), cUerId);
			setAttr("postPage", postPage);
		}
		//我的评论
		else if (page.equals("comments")) {
			Page<PostsComment> commentPage = commentSrv.getCommentAndPost(getParaToInt("p", 1), cUerId);
			setAttr("commentPage", commentPage);
		}
		//我的收藏
		else if (page.equals("favors")) {
			Page<Posts> postPage = homeSrv.getFavorPosts(cUerId, getParaToInt("p", 1));
			setAttr("postPage", postPage);
		}
		//我的关注
		else if(page.equals("follows")) {
			Page<Friend> followPage = homeSrv.getFollows(cUerId, getParaToInt("p", 1));
			setAttr("followPage", followPage);
		}
		//我的粉丝
		else if (page.equals("fans")) {
			Page<Friend> fansPage = homeSrv.getFans(cUerId, getParaToInt("p", 1));
			setAttr("fansPage", fansPage);
		}
		//@到我
		else if (page.equals("referMe")) {
			Page<NewsFeed> newsFeedPage = referMeService.paginate(getLoginAccountId(), getParaToInt("p", 1));
			// 重置提醒 remind 的 referMe 字段
			remindService.resetRemindOfReferMe(getLoginAccountId());
			setAttr("newsFeedPage", newsFeedPage);
		}
		//通知
		else if (page.equals("notifies")) {
			Page<Message> msgPage = msgSrv.paginate(getParaToInt("p", 1), cUerId);
			//用户查看消息之后重置消息数量,这里暂时只有message
			//TODO fans,referMe后面需要增加
			remindSrv.resetRemindOfMessage(cUerId);
			remindSrv.resetRemindOfNewFans(cUerId);
			remindSrv.resetRemindOfReferMe(cUerId);
			
			setAttr("msgPage", msgPage);
		}
		//私信
		else if (page.equals("message")) {
			int userId = getLoginAccountId();
			Page<Message> messagePage = msgSrv.paginate(getParaToInt("p", 1), userId);
			remindService.resetRemindOfMessage(userId);
			setAttr("messagePage", messagePage);
		}
		
		render("index.html");
	}
}
